package com.atguigu.spring.config;

import com.atguigu.spring.entity.Result;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    public static final int ARITH_ERROR_CODE = 55555;
    public static final int GLOBAL_ERROR_CODE = 11111;

    // 声明当前方法要处理的异常类型
    @ExceptionHandler(value = ArithmeticException.class)
    public Result<Void> arithmeticExceptionHandler(
            // 接收 SpringMVC 框架帮我们捕获到的异常
            ArithmeticException exception) {
        return Result.failed(ARITH_ERROR_CODE, exception.getMessage());
    }

    // 声明当前方法映射 Exception 类型的异常
    @ExceptionHandler(value = Exception.class)
    public Result<Void> exceptionHandler(Exception exception) {
        return Result.failed(GLOBAL_ERROR_CODE, exception.getMessage());
    }
}
